package com.xneure.database.define.resolver;

import java.sql.Connection;
import java.sql.SQLException;
import com.xneure.database.define.Creator;
import com.xneure.database.define.TableDefine;

public class DefineHandler{
	private TableDefine[] tableDefines;

	public void resolverXml(String definePath){
		MapResolver resolver = new XmlMapResolver(DefineHandler.class.getResourceAsStream(definePath));
		tableDefines = resolver.getTableDefines();
	}

	public void createTable(Connection conn){
		createTable(conn,tableDefines);
	}

	public void createTable(Connection conn,TableDefine... tabs){
		try{
			Creator c = getCreator(conn);
			c.setForcedUpdate(true);
			for(TableDefine td:tableDefines){
				if(c.create(conn,td)){
					System.out.println("创建成功");
				}else{
					System.out.println("创建失败");
				}
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	protected Creator getCreator(Connection conn) throws SQLException{
		String name = conn.getMetaData().getDatabaseProductName().toLowerCase();
		Creator c = Creator.getCreator(name);
		return c;
	}
}
